﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ControllerFree : MonoBehaviour {
	private Transform cameraObject;
	public bool isActive = false;
	private Vector2 moveDir = new Vector2(1.0f, 1.0f);
	// 最近距离、最远距离
	private static float s_cameraNear = 10.0f;
	private static float s_cameraFar = 60.0f;
	private float cameraHeight = 20.0f;
	private float speed = 5.0f;
	private float zoom = 5.0f;
	private ControllerMaster MC;
	private InputController IC;
	// Use this for initialization
	void Start () {
	}
	void Awake() {
		MC = this.gameObject.GetComponent<ControllerMaster>() as ControllerMaster;
		IC = this.gameObject.GetComponent<InputController>() as InputController;
	}
	void FixedUpdate()
    {		
		if (isActive){
			cameraObject = MC.cameraObject;
			if (IC.inputKeyW) moveDir.y = 1.0f;
			else if (IC.inputKeyS) moveDir.y = -1.0f;
			else moveDir.y = 0.0f;
			if (IC.inputKeyA) moveDir.x = -1.0f;
			else if (IC.inputKeyD) moveDir.x = 1.0f;
			else moveDir.x = 0.0f;			
			
			Vector3 position = new Vector3();

			position.x = Mathf.Lerp(0.0f, speed*moveDir.x, Time.deltaTime);
			position.y = Mathf.Lerp(0.0f, speed*moveDir.y, Time.deltaTime);
			float fOldHeight = cameraObject.transform.localPosition.y;
			cameraObject.transform.Translate(position);
			position = cameraObject.transform.localPosition;
			position.y = fOldHeight;
			cameraObject.transform.localPosition = position;
			
			position = Vector3.zero;
			float oldCameraHeight = cameraHeight;
			cameraHeight -= zoom*IC.inputMouseWheel;
			if(cameraHeight < s_cameraNear)
			{
				cameraHeight = s_cameraNear;
			}
			if(cameraHeight > s_cameraFar)
			{
				cameraHeight = s_cameraFar;
			}
			position.z = oldCameraHeight-cameraHeight;
			cameraObject.transform.Translate(position);
		}
    }
}
